package com.zhangwei.day40share.login;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;


import com.zhangwei.day40share.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

public class ThreeLoginActivity extends ActionBarActivity implements View.OnClickListener {
    private Button weiboBtn;
    private Button qqBtn;
    private Button weiChatBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three_login);
        ShareSDK.initSDK(getApplicationContext());
        initView();
    }

    private void initView() {
        weiboBtn = (Button) findViewById(R.id.sina_login_btn);
        qqBtn = (Button) findViewById(R.id.qq_login_btn);
        weiChatBtn = (Button) findViewById(R.id.weichat_login_btn);
        weiboBtn.setOnClickListener(this);
        qqBtn.setOnClickListener(this);
        weiChatBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Platform platform = null;
        switch (v.getId()) {
            case R.id.sina_login_btn:
                platform = ShareSDK.getPlatform(SinaWeibo.NAME);
                break;
            case R.id.qq_login_btn:
                platform = ShareSDK.getPlatform(QQ.NAME);
                break;
            case R.id.weichat_login_btn:
                platform = ShareSDK.getPlatform(Wechat.NAME);
                break;
        }
        threeLogin(platform);


    }

    /**
     * 第三方登陆实现方法
     * 1> 获取平台
     */
    public void threeLogin(Platform platform) {

        platform.removeAccount();
        platform.removeAccount(true);
        if (platform == null) {
            return;
        }
        PlatformDb db = platform.getDb();
        if (db.isValid()) {
            //一种已经认证过并且没过期
            String id = db.getUserId();
            String name = db.getUserName();
            String url = db.getUserIcon();
            return;
        }

        /**
         *没有认证
         */
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.e("----->>>", hashMap.toString());
                String name = (String) hashMap.get("name");
                String userIcon = (String) hashMap.get("profile_image_url");
                String id = (String) hashMap.get("id");


            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        platform.showUser(null);
    }

    /**
     * 调用本地的登陆方法请求自己的服务器
     * @param name
     * @param id
     */
    public void login(String name, String id) {

        new Thread() {
            @Override
            public void run() {
                //如果成功 直接服务器返回用户信息  未绑定本地用户系统

                //失败根据相应服务器的状态吗处理
            }
        }.start();


    }


}
